<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<div>
    <span id="encrypt"></span><br>
    <span id="decrypt"></span>
</div>
<script src="webjars/jquery/3.3.1-1/jquery.min.js"></script>
<script src="webjars/cryptojs/3.1.2/rollups/aes.js"></script>
<script src="webjars/cryptojs/3.1.2/rollups/pbkdf2.js"></script>
<script src="AesUtil.js"></script>
<script type="application/javascript">
    window.onload = function () {
        var data = "您好,在不设置取消规则时，则默认没有取消处罚，均全额退款，设置后，则按规则进行退款；";
        /*var key = CryptoJS.enc.Base64.parse('ZGIyMTM5NTYxYzlmZTA2OA==');
        var iv  = CryptoJS.enc.Base64.parse('ZGIyMTM5NTYxYzlmZTA2OA==');

        function encrypt(data, key, iv) {
            return CryptoJS.AES.encrypt(data, key, {
                iv: iv,
                padding: CryptoJS.pad.Pkcs7,
                model:CryptoJS.mode.CBC
            });
        }

        function decrypt(data, key, iv) {
            return CryptoJS.AES.decrypt(data, key, {
                iv: iv,
                padding: CryptoJS.pad.Pkcs7,
                model:CryptoJS.mode.CBC
            })
        }*/

        var aesUtil = new AesUtil();
        var salt =  aesUtil.random();
        var iv   =  aesUtil.random();
        var encrypt = aesUtil.encrypt(salt, iv, data);
        var decrypt = aesUtil.decrypt(salt, iv, encrypt);
        $("#encrypt").html(encrypt);
        $("#decrypt").html(decrypt);
        console.log(CryptoJS.enc.Hex.parse("xxxxxxxxxx"))

        $.ajax("/encrypt", {
            type: "get",
            data: {'plaintext':"你好"},
            success: function (res) {
                var encrypt = aesUtil.decrypt(res.salt, res.iv, res.encrypt);
                console.log("salt："+res.salt);
                console.log("iv："+res.iv);
                console.log("前端解密："+encrypt);
                $.ajax("/decrypt", {
                    type: "get",
                    data: {'salt':res.salt,iv: res.iv,"ciphertext":res.encrypt},
                    success: function (res) {
                        console.log("后端解密："+res.decrypt);
                    }
                })
            }
        })
        var param = {'id':1,'name':salt,'age': iv,"sex":encrypt};
        var encryptParam = aesUtil.encrypt(salt, iv, JSON.stringify(param));
        encryptParam = "**ENCRYPT**"+salt+iv+encryptParam;
        console.log("encryptParam"+encryptParam);
        $.ajax("/postJson", {
            type: "post",
            data: encryptParam,
            success: function (res) {
                console.log("postJson："+res);
            }
        })

    };



</script>
</body>
</html>